package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Employee;
import cn.wolfcode.wms.domain.OrderBill;
import cn.wolfcode.wms.domain.OrderBillItem;
import cn.wolfcode.wms.mapper.OrderBillItemMapper;
import cn.wolfcode.wms.mapper.OrderBillMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.IOrderBillService;
import cn.wolfcode.wms.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

@Service
public class OrderBillService implements IOrderBillService {



    @Autowired
    private OrderBillMapper orderBillMapper;
    @Autowired
    private OrderBillItemMapper orderBillItemMapper;

    public int deleteByPrimaryKey(Long id) {
        return orderBillMapper.deleteByPrimaryKey(id);
    }

    public int insert(OrderBill record) {
        return orderBillMapper.insert(record);
    }

    public OrderBill selectByPrimaryKey(Long id) {
        return orderBillMapper.selectByPrimaryKey(id);
    }

    public List<OrderBill> selectAll() {
        return orderBillMapper.selectAll();
    }

    public int updateByPrimaryKey(OrderBill record) {
        return orderBillMapper.updateByPrimaryKey(record);
    }


    public void saveorupdate(OrderBill orderbill) {

        if (orderbill.getId() != null) {
            update(orderbill);
        } else {
            save(orderbill);
        }
    }


    private void update(OrderBill orderbill) {
        //1.删除关系
        orderBillItemMapper.deleteByBillId(orderbill.getId());
        //2.计算商品总金额与总数量
        BigDecimal totalAmount =  BigDecimal.ZERO;
        BigDecimal totalNumber =  BigDecimal.ZERO;
        //3.遍历订单明细,计算明细金额小计
         for (OrderBillItem item : orderbill.getItems() ) {
             //4.为明细设置订单id
             item.setBillId(item.getId());
             //5.跟新明细
             BigDecimal amount = item.getNumber().multiply(item.getCostPrice()).setScale(2, BigDecimal.ROUND_HALF_UP);
             item.setAmount(amount);
             orderBillItemMapper.insert(item);
             //6.累加总金额
            totalAmount = totalAmount.add(amount);
            totalNumber.add(item.getNumber());
         }
        //7.累加总金额
        orderbill.setTotalNumber(totalNumber);
        orderbill.setTotalAmount(totalAmount);
        //8.跟新订单
        orderBillMapper.updateByPrimaryKey(orderbill);
        orderBillMapper.insert(orderbill);
    }
    private void save (OrderBill orderBill){
        //获取当前用户
        Employee currentUser = UserContext.getCurrentUser();
        orderBill.setInputUser(currentUser);
        orderBill.setInputTime(new Date());
        //2.重置订单状态
        orderBill.setStatus(OrderBill.ORDER_BILL_STATUS_NORMAL);
        //3.计算总金额与总数量
        BigDecimal totalAmount = BigDecimal.ZERO;
        BigDecimal totalNumber = BigDecimal.ZERO;
        //4.遍历明细列表,用于计算
        for (OrderBillItem item :orderBill.getItems() ) {
            //计算明细的金额小计,设置到明细对象中
            BigDecimal amonut = item.getNumber().multiply(item.getCostPrice()).setScale(2, RoundingMode.HALF_UP);
            item.setAmount(amonut);

            totalAmount = totalAmount.add(amonut);
            totalAmount = totalNumber.add(item.getNumber());
        }
        //6为订单对象设置设置总金额和总数量
        orderBill.setTotalAmount(totalAmount);
        orderBill.setTotalNumber(totalNumber);
        //保存订单
        orderBillMapper.insert(orderBill);
        //为订单保存明细

         for (OrderBillItem item :orderBill.getItems() ) {
             item.setBillId(orderBill.getId());
             orderBillItemMapper.insert(item);
         }
    }




    public void delete(Long id) {
        if(id != null){
            orderBillItemMapper.deleteByBillId(id);
            orderBillMapper.deleteByPrimaryKey(id);
        }
    }

    public PageResult query(QueryObject qo) {
        int count = orderBillMapper.queryForCount(qo);
        if(count == 0){
            return  PageResult.empty(qo.getPageSize());
        }
        List<?> list = orderBillMapper.queryForList(qo);

        PageResult pageResult = new PageResult(qo.getPageSize(), count, qo.getCurrentPage(), list);


        return pageResult;
    }

    public void audit(Long id) {
        //先通过id查询当前订单
        OrderBill orderBill = orderBillMapper.selectByPrimaryKey(id);
        //判断订单是否审核
        if(orderBill != null && OrderBill.ORDER_BILL_STATUS_NORMAL == orderBill.getStatus() ){
            //跟新审核相关信息
            orderBill.setAuditor(UserContext.getCurrentUser());
            orderBill.setAuditTime(new Date());

            orderBill.setStatus(OrderBill.ORDER_BILL_STATUS_AUDITED);


            //跟新订单
            orderBillMapper.autid(orderBill);
        }


        
        

    }
}

